package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.io.IOException;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import modele.CKModel;
import application.Consts;
import controleur.ControleBtnK0;

public class PanelK0 extends JPanel {

	
	static final long serialVersionUID = 12345;

	// NLP Crap
	private final String stopWordsCSV = "a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your";
	
	CKModel<String, String> _modele;
	VueEditeurCK _vue;
	
	private JLabel lblK0;
	private JTextArea txtK0;
	private JScrollPane scp;
	private JLabel lblErrorK0;
	private JButton btnValiderK0;
	
	
	public PanelK0(CKModel<String, String> modele, VueEditeurCK vue){

		this._modele = modele;
		this._vue = vue;
		
		this.setLayout(new BorderLayout(10,10));
		
		JLabel lblC0= new JLabel("C0: "+_modele.getCSpace().getData(), JLabel.CENTER);
		this.add(lblC0, BorderLayout.PAGE_START);
		
		lblK0 = new JLabel(Consts.initialK);
		this.add(lblK0, BorderLayout.LINE_START);
		
		
		// NLP crap
		StringTokenizer st = new StringTokenizer(_modele.getCSpace().getData().toLowerCase());
		int iK = 0;
		String defaultString = "";
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			if (!stopWordsCSV.contains(token)) {
				//defaultString += "Knowledge about: ";
				defaultString += token;
				defaultString += Consts.NEW_LINE;
				defaultString += "-------";
				defaultString += Consts.NEW_LINE;
				defaultString += Consts.NEW_LINE;
			}
			
		}
		
		txtK0 = new JTextArea(defaultString);
		txtK0.setLineWrap(true);
		scp = new JScrollPane(txtK0, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
		        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scp.setPreferredSize(new Dimension(100,100));
		this.add(txtK0, BorderLayout.CENTER);
		txtK0.requestFocusInWindow();
		
		lblErrorK0 = new JLabel("");
		lblErrorK0.setPreferredSize(new Dimension(200,30));
		lblErrorK0.setForeground(Color.RED);
		this.add(lblErrorK0, BorderLayout.LINE_END);
		
		btnValiderK0 = new JButton("Valider");
		btnValiderK0.setPreferredSize(new Dimension(200,30));
		btnValiderK0.addActionListener(new ControleBtnK0(_modele, _vue));
		this.add(btnValiderK0, BorderLayout.PAGE_END);
		
		
	}
	
	public String getTextK0(){
		return this.txtK0.getText();
	}
	
	
	public JButton getBtnValider(){
		return this.btnValiderK0;
	}
	


	/**
	 * @param lblErrorK0 the lblErrorK0 to set
	 */
	public void setLblErrorK0(String erreur) {
		this.lblErrorK0.setText(erreur);
	}


	/**
	 * @return the lblErrorK0
	 */
	public JLabel getLblErrorK0() {
		return lblErrorK0;
	}
	
	/*
	 * New function here for debugging, will need to be moved later on
	 */
	/**
	 * Reads file of words to ignore. Ignore words are added to a Set. The
	 * IOException is passed to caller because we certainly don't know what the
	 * user interface issue is.
	 * 
	 * @param ignoreString
	 *            File of words to ignore.
	 */
	public Set<String> ignore(String ignoreString) throws IOException {
		Scanner ignoreScanner = new Scanner(ignoreString);
		ignoreScanner.useDelimiter("[^A-Za-z]+");

		Set<String> stopWords = null;
		while (ignoreScanner.hasNext()) {
			stopWords .add(ignoreScanner.next());
		}
		ignoreScanner.close(); // Close underlying file.
		return stopWords;
	}
	
}